import Taro from '@tarojs/taro';
import { getUserInfo } from '../pages/login/service';

export default {
  namespace: 'common',
  state: {
    cartChecked:true,
    cartItems: [],
    newState: {},
    data:''
  },

  effects: {
    * init({ payload }, { call }) {
      console.log('common', 'init');
      try {
        yield Taro.checkSession();
        // session_key 未过期，并且在本生命周期一直有效
        console.log('wechatLogin', 'payload', payload);
        const response = yield call(getUserInfo);
        console.log('response--------------------', response.bound);
        if (response.bound === false) {
          Taro.switchTab({ url: '../home/index' });
        } else {
          Taro.switchTab({ url: '../home/index' });
        }
      } catch (e) {
        // session_key 已经失效，需要重新执行登录流程
        console.log('e', e);
        // yield wechatLogin();
      }
    },
    * wechatLogin({ payload }, { call }) {
      console.log('wechatLogin', 'payload', payload);
      const response = yield call(getUserInfo);
      if (response.bound === false) {
        Taro.switchTab({ url: '../home/index' });
      } else {
        Taro.switchTab({ url: '../home/index' });
      }
    },

  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
